package com.skyworth.web.wechat.service.impl;

import com.skyworth.utils.StrUtil;
import com.skyworth.web.wechat.common.AppSecretInfo;
import com.skyworth.web.wechat.common.EmojiFilter;
import com.skyworth.web.wechat.engin.ServerEngin;
import com.skyworth.web.wechat.entity.AccessTokenModel;
import com.skyworth.web.wechat.entity.PayDetailModel;
import com.skyworth.web.wechat.entity.TicketModel;
import com.skyworth.web.wechat.entity.WxUserInfoModel;
import com.skyworth.web.wechat.repository.AccessTokenRepository;
import com.skyworth.web.wechat.repository.PayDetailRepository;
import com.skyworth.web.wechat.repository.TicketRepository;
import com.skyworth.web.wechat.repository.WxUserInfoRepository;
import com.skyworth.web.wechat.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by yuhaichao on 2016/8/24.
 */
@Service
public class AccessTokenServiceImpl implements AccessTokenService{

    @Autowired
    private AccessTokenRepository accessTokenRepository;

    @Autowired
    private TicketRepository ticketRepository;

    @Autowired
    private WxUserInfoRepository wxUserInfoRepository;

    @Autowired
    private PayDetailRepository payDetailRepository;


    @Override
    public AccessTokenModel saveToken(AccessTokenModel at) {
        return accessTokenRepository.save(at);
    }

    @Override
    public AccessTokenModel findByAppId(String appId) {
        return accessTokenRepository.findOne(appId);
    }

    @Override
    public TicketModel saveTicket(TicketModel tm) {
        return ticketRepository.save(tm);
    }

    @Override
    public TicketModel findTicketByAppId(String appId) {
        return ticketRepository.findOne(appId);
    }

    @Override
    public WxUserInfoModel saveWxUserInfo(String userId) {
        AccessTokenModel tm =  accessTokenRepository.findOne(AppSecretInfo.appId);
        Map<Object,Object> userInfoMap = ServerEngin.getInstance().getUserInfo(tm.getAccessToken(),userId);
        if(userInfoMap!=null && userInfoMap.get("nickname")!=null){
            String nickname= StrUtil.objectToString(userInfoMap.get("nickname"));
            nickname= EmojiFilter.filterEmoji(nickname);
            WxUserInfoModel um = new WxUserInfoModel();
            um.setOpenid(StrUtil.objectToString(userInfoMap.get("openid")));
            um.setNickname(nickname);
            um.setAppid(AppSecretInfo.appId);
            um.setCity(StrUtil.objectToString(userInfoMap.get("city")));
            um.setCountry(StrUtil.objectToString(userInfoMap.get("country")));
            um.setHeadimgurl(StrUtil.objectToString(userInfoMap.get("headimgurl")));
            um.setProvince(StrUtil.objectToString(userInfoMap.get("province")));
            um.setSex(StrUtil.objectToString(userInfoMap.get("sex")));

            return wxUserInfoRepository.save(um);
        }
        return null;
    }

    @Override
    public WxUserInfoModel findUserByOpenid(String openid) {
        return wxUserInfoRepository.findOne(openid);
    }

    @Override
    public PayDetailModel savePayDetail(PayDetailModel pdm) {
        return payDetailRepository.save(pdm);
    }

}
